﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSite.Core;
using Common;
using Shanda.Utility;

namespace WebSite
{
    public partial class LookingForTeam : BasePage
    {
        public List<Maticsoft.Model.CarrefourCover.Baby> listSearchByName
        {
            get;
            set;
        }

        public List<Maticsoft.Model.CarrefourCover.Baby> listSearchByType
        {
            get;
            set;
        }

        public int RowTotal { get; set; }
        public new int PageIndex { get { return WebUtils.GetPostValue(Request, "PageIndex", 0); } }
        public new int PageSize { get { return 2; } }

        public int rRowTotal { get; set; }
        public new int rPageIndex { get { return WebUtils.GetPostValue(Request, "rPageIndex", 0); } }
        public new int rPageSize { get { return 9; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            NavHelper.SetNavMenuSelectedIndex(1);
            NavHelper.SetNavStepCurrentStep(3);

            GetTeamByName();
            GetTeamBySort();
        }

        const string BASE_QUERY = "AND status=1 AND (ConfirmTeamId IS NULL OR ConfirmTeamId = 0) ";

        private void GetTeamByName()
        {
            string query = StringHelper.Convert(Request["Query"], string.Empty);
            Maticsoft.BLL.CarrefourCover.Baby bll = new Maticsoft.BLL.CarrefourCover.Baby();

            if (string.IsNullOrEmpty(query))
            {
                listSearchByName = new List<Maticsoft.Model.CarrefourCover.Baby>();
                RowTotal=0;
                return;
            }

            int total;
            listSearchByName = bll.GetListByPage( PageSize, PageIndex + 1, BASE_QUERY + "AND Id <> " + Project_Session.CurrentUserID + " AND BabyName = '" + query.SqlFilter() + "'", "ORDER BY ID asc", out total );
            RowTotal = total;

            if (listSearchByName == null)
            {
                listSearchByName = new List<Maticsoft.Model.CarrefourCover.Baby>();
            }
        }

        private void GetTeamBySort()
        {
            int type = StringHelper.Convert<int>(Request["rtype"], 3);
            
            Maticsoft.BLL.CarrefourCover.Baby bll = new Maticsoft.BLL.CarrefourCover.Baby();

            int total = 0;

            switch (type)
            {
                case 1:
                    DateTime birthday = Project_Session.CurrentUserBaby == null ? DateTime.Now : NullableHelper.CastValueType(Project_Session.CurrentUserBaby.BabyBirthday);
                    listSearchByType = bll.GetListByPage( rPageSize, rPageIndex + 1, BASE_QUERY + "AND Id <> " + Project_Session.CurrentUserID + " AND BabyBirthday >= '" 
                        + birthday.AddMonths(-6).ToString("yyyy-MM-dd HH:mm:ss") + "' AND BabyBirthday <= '" + birthday.AddMonths(6).ToString("yyyy-MM-dd HH:mm:ss") + "'", "ORDER BY CREATETIME DESC", out total);
                    break;
                case 2:
                    string province = Project_Session.CurrentUserBaby == null ? "" : Project_Session.CurrentUserBaby.LiveProvince;
                    listSearchByType = bll.GetListByPage( rPageSize, rPageIndex + 1, BASE_QUERY + "AND Id <> " + Project_Session.CurrentUserID + " AND LiveProvince = '" + province.SqlFilter() + "'", "ORDER BY CREATETIME DESC", out total );
                    break;
                case 3:
                    listSearchByType = bll.GetListByPage( rPageSize, rPageIndex + 1, BASE_QUERY + "AND Id <> " + Project_Session.CurrentUserID, "ORDER BY CREATETIME DESC", out total );
                    break;
            }
            rRowTotal = total;

            if (listSearchByType == null)
            {
                listSearchByType = new List<Maticsoft.Model.CarrefourCover.Baby>();
            }
        }
    }
}